package com.example.zzy.zzytest.data.structure.sparsearray;

import java.io.*;
import java.util.Arrays;

public class SparseArray {
    public static void main(String[] args) {
        //11*11棋盘 1黑 2白
        int[][] chessArr = new int[11][11];
        chessArr[1][2] = 1;
        chessArr[2][3] = 2;
        chessArr[3][4] = 2;
        chessArr[4][5] = 1;

        System.out.println("原始棋盘输出>>>");
        SparseArray.printChess(chessArr);

        System.out.println("转稀疏数组>>>");
        int[][] sparseArr = chessArray2sparseArray(chessArr);
        printChess(sparseArr);

        //稀疏数组存盘
        storage(sparseArr, "E:\\hadoop\\zzttest\\storage\\SparseArray.txt");

        //读取稀疏数组
        int[][] tempArr = getStoragedArr("E:\\hadoop\\zzttest\\storage\\SparseArray.txt");
        System.out.println("转二维数组");
        int[][] finalArr = sparseArray2ChessArray(tempArr);
        printChess(finalArr);
    }

    /**
     * // 将二维数组 转 稀疏数组
     *
     * @param chessArr
     * @return
     */
    public static int[][] chessArray2sparseArray(int[][] chessArr) {
        int count = 0;
        for (int[] row : chessArr) {
            for (int item : row) {
                if (item != 0) {
                    count++;
                }
            }
        }

        int[][] res = new int[count + 1][3];
        res[0][0] = chessArr.length;
        res[0][1] = chessArr[0].length;
        res[0][2] = count;

        int index = 1;
        for (int i = 0; i < chessArr.length; i++) {
            for (int j = 0; j < chessArr[0].length; j++) {
                if (chessArr[i][j] != 0) {
                    res[index][0] = i;
                    res[index][1] = j;
                    res[index][2] = chessArr[i][j];
                    index++;
                }
            }
        }

        return res;
    }

    /**
     * 稀疏数组 转 二维数组
     *
     * @param spaArr
     * @return
     */
    public static int[][] sparseArray2ChessArray(int[][] spaArr) {
        int row = spaArr[0][0];
        int column = spaArr[0][1];

        int[][] res = new int[row][column];
        for (int i = 1; i < spaArr.length; i++) {
            res[spaArr[i][0]][spaArr[i][1]] = spaArr[i][2];
        }
        return res;
    }


    /**
     * 打印数组，返回非0值得个数
     *
     * @param chessArr
     * @return
     */
    public static int printChess(int[][] chessArr) {
        int count = 0;
        for (int[] row : chessArr) {
            for (int item : row) {
                System.out.printf("%d\t", item);
                if (item != 0) {
                    count++;
                }
            }
            System.out.println("\n");
        }
        return count;
    }

    /**
     * 数组落地磁盘
     *
     * @param arr
     */
    public static void storage(int[][] arr, String path) {
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(new File(path)));
            //一行一行写入
            for (int[] row : arr) {
                String s = Arrays.toString(row);
                s = s.substring(1, s.length() - 1);
                bufferedWriter.write(s);
                bufferedWriter.write("\n");
            }

            //一个一个写入
//            for (int[] row : arr) {
//                for (int item : row) {
//                    bufferedWriter.write(item + "\t");
//                }
//                bufferedWriter.write("\n");
//            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 从磁盘读取数组
     *
     * @param path
     * @return
     */
    public static int[][] getStoragedArr(String path) {
        int[][] sparseArr = new int[5][3];

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //一行一行读取
        String line = null;
        int index = 0;
        while (true) {
            try {
                if ((line = reader.readLine()) == null) break;
            } catch (Exception e) {
                e.printStackTrace();
            }

            String[] split = line.split(",");
            for (int i = 0; i < split.length; i++) {
                sparseArr[index][i] = Integer.parseInt(split[i].trim());
            }
            index++;
        }
        return sparseArr;
    }
}